package com.cx.controller;

import com.cx.common.ApiReturn;
import com.cx.pojo.Recommend;
import com.cx.service.RecommendService;
import com.cx.vo.RecommendPojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionContext;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("recommend")
@CrossOrigin//跨域
public class RecommendController {
    @Autowired
    RecommendService recommendService;

    @RequestMapping("insertRecommend")
    @ResponseBody
    public ApiReturn insertRecommend(RecommendPojo recommendPojo, HttpSession session){
        //照片怎么传？直接接受一个Recommend的javaBean对象就行了（参数太多传对象方便）
        //不行，前端传入的并不是一个完整的javaBean对象

        String recNews = recommendPojo.getRecCity();
        String recCity = recommendPojo.getRecCity();
        String recDistrict = recommendPojo.getRecDistrict();
        String recStreet = recommendPojo.getRecStreet();

        String name =  recommendPojo.getRecPhoto().getOriginalFilename();
        String fileName = UUID.randomUUID() + name.substring(name.lastIndexOf("."));
        File file = new File(fileName);
        String recPhoto = "http://locahost:8888/img/" + fileName;

        try {
            // "http://locahost:8080/img/" + fileName
            // 自行查阅修改tomcat默认端口号 -> server.xml
            // 保存到 配置文件中配置的保存路径
            recommendPojo.getRecPhoto().transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }

        Date date = new Date();
        Integer userId = (Integer)session.getAttribute("userId");

        Recommend recommend = null;
        recommend.setRecNews(recNews);
        recommend.setRecCity(recCity);
        recommend.setRecDistrict(recDistrict);
        recommend.setRecStreet(recStreet);
        recommend.setRecDate(date);
        recommend.setRecPhoto(recPhoto);
        recommend.setUserId(userId);
        recommend.setStatus(1);

        int rows = recommendService.insert(recommend);
        if(rows != 0){
            return ApiReturn.success("插入成功");
        }else {
            return ApiReturn.error("插入失败");
        }

    }

    @RequestMapping("updateRecommend")
    @ResponseBody
    public ApiReturn updateRecommend(Integer recId,Recommend recommend){
       int rows = recommendService.updateById(recId,recommend);
        if(rows != 0){
            return ApiReturn.success("更新成功");
        }else {
            return ApiReturn.error("更新失败");
        }
    }

    @RequestMapping("deleteRecommend")
    @ResponseBody
    public ApiReturn deleteRecommend(Integer recId){
        int rows = recommendService.deleteById(recId);
        if(rows != 0){
            return ApiReturn.success("删除成功");
        }else{
            return ApiReturn.error("删除失败");
        }
    }

    @RequestMapping("selectByRecCity")
    @ResponseBody
    public  ApiReturn selectByRecCity(String recCity){
        List<Recommend> recommend = recommendService.selectByRecCity(recCity);
        if(recommend != null){
            return ApiReturn.success("按城市查询结果",recommend);
        }else {
            return ApiReturn.error("此城市无车展或查询失败");
        }
    }

    @RequestMapping("selectByRecDate")
    @ResponseBody
    public ApiReturn selectByRecDate(Date recDate){
        List<Recommend> recommends = recommendService.selectByRecDate(recDate);
        if(recommends != null){
            return ApiReturn.success("按日期查询结果",recommends);
        }else {
            return ApiReturn.error("此日期无车展或查询失败");
        }
        }

    @RequestMapping("selectAll")
    @ResponseBody
    public ApiReturn selectAll(){
        List<Recommend> recommends = recommendService.selectAll();
        if(recommends != null){
            return ApiReturn.success("所有车展信息",recommends);
        }else {
            return ApiReturn.error("查询失败");
        }
    }
}
